package com.jumi.microservice.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.GoodsKeyConstant;
import com.jumi.microservice.constant.StatusEnum;
import com.jumi.microservice.domain.ActivityGoodsBrandDTO;
import com.jumi.microservice.domain.BrandDO;
import com.jumi.microservice.domain.CommodityRenderApplyDTO;
import com.jumi.microservice.domain.OrderItemDTO;
import com.jumi.microservice.service.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * - - - - - - - - - - - - - - -
 *
 * @author liuz
 * @date 2020/12/16 14:17
 * ToDoSomeThing
 * - - - - - - - - - - - - - - -
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @DubboReference
    CommodityApi commodityApi;

    @DubboReference
    BasicDataService basicDataService;

    @DubboReference
    InventoryApi inventoryApi;

    @DubboReference
    IOrderInventoryService orderInventoryService;

    @Autowired
    BrandService brandService;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/skuIds")
    public ResponseResult test1(){
        String res = null;
        List<Long> ids = new ArrayList<>();
        ids.add(230L);
        return ResultGenerator.genSuccessResult(basicDataService.getSkuInfoByIds(ids));
    }

    @GetMapping("/spuIds")
    public ResponseResult test2(){
        return ResultGenerator.genSuccessResult(commodityApi.getPrivacyGoodsSPuIds());
    }

    @PostMapping("/test3")
    public ResponseResult test3(){
        String res = "{\"commodityRenderDTOS\":[{\"applyId\":237,\"createBy\":\"管理员\",\"id\":438,\"renderStockNum\":10,\"skuCode\":\"SKU_1344478380930744320\",\"skuId\":272,\"type\":0,\"warehouseId\":31,\"warehouseName\":\"山东仓4\"}],\"details\":[{\"applyId\":237,\"id\":380,\"leftPreNum\":10,\"materialNum\":0,\"preStockNum\":10,\"renderStockNum\":0,\"skuCode\":\"SKU_1344478380930744320\",\"skuId\":272,\"specName\":\"鼠干鼠干 黄色\",\"stockNum\":10}],\"spuId\":197}";
        CommodityRenderApplyDTO commodityRenderApplyDTO = JSONObject.toJavaObject(JSONObject.parseObject(res),CommodityRenderApplyDTO.class);
        inventoryApi.supplySubWarehouse(commodityRenderApplyDTO);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/test4")
    public ResponseResult test4() throws Exception {
        String res = "[{\"areaId\":210000,\"goodsSkuId\":380,\"purchaseQuantity\":1,\"requestTime\":1610965311115,\"userAccountId\":94},{\"areaId\":210000,\"goodsSkuId\":379,\"purchaseQuantity\":1,\"requestTime\":1610965311115,\"userAccountId\":94}]";
        JSONArray array = JSONObject.parseArray(res);
        List<OrderItemDTO> var = new ArrayList<>();
        array.stream().forEach(js->{
            OrderItemDTO orderItemDTO = JSONObject.toJavaObject(JSONObject.parseObject(js.toString()),OrderItemDTO.class);
            var.add(orderItemDTO);
        });
        orderInventoryService.informSubmitOrderEvent(var);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 更新redis
     * @return
     */
    @GetMapping("/test5")
    public ResponseResult test5(){
        Object o = redisTemplate.opsForValue().get(GoodsKeyConstant.ACYTIVITY_GOODS_BRAND);
        if(ObjectUtil.isNull(o)){
            List<BrandDO> list = brandService.list(
                    Wrappers.<BrandDO>lambdaQuery()
                            .eq(BrandDO::getIsDel, StatusEnum.STATUS_NO.getCode()));
            List<ActivityGoodsBrandDTO> brands = list.stream().map(lst->{
                ActivityGoodsBrandDTO activityGoodsBrandDTO = new ActivityGoodsBrandDTO();
                activityGoodsBrandDTO.setId(lst.getId());
                activityGoodsBrandDTO.setBrandName(lst.getBrandName());
                activityGoodsBrandDTO.setBrandSlogan(lst.getBrandSlogan());
                activityGoodsBrandDTO.setLogo(lst.getLogo());
                activityGoodsBrandDTO.setOrderNum(lst.getOrderNum());
                return activityGoodsBrandDTO;
            }).collect(Collectors.toList());
            redisTemplate.opsForValue().set(GoodsKeyConstant.ACYTIVITY_GOODS_BRAND, brands);
        }

        return ResultGenerator.genSuccessResult();
    }
}
